﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    // Caméra du jeu (construite pour se déplacer que sur l'axe X)
    public class Camera
    {
        public Camera(int min, int max, int width)
        {
            if (max - min < width)
                throw new Exception("Erreur : la vue est plus large que la map.");
            this.Min = min;
            this.Max = max;
            this.ViewWidth = width;
            this.pos = 0;
            this.IsScrolling = true;
        }

        public int Position
        {
            get
            {
                return this.pos;
            }
            set
            {
                if (value > Min && value < Max - ViewWidth)
                    this.pos = value;
            }
        }

        private int pos;
        public int ViewWidth;
        private int Min;
        private int Max;
        public bool IsScrolling;

        public void AutoScroll(int index1, List<Unit> list1, int index2, List<Unit> list2)
        {
            if (list1.Count() > index1 && list2.Count() > index2)
            {
                if (list1[index1].DeadTime == 0)
                {
                    if (list2[index2].DeadTime == 0)
                    {
                        if (IsScrolling)
                        {
                            if (Position + (ViewWidth / 2) < ((list1[index1].Position.X + list2[index2].Position.X) / 2) - 10)
                                Position += 2;
                            else
                                if (Position + (ViewWidth / 2) > ((list1[index1].Position.X + list2[index2].Position.X) / 2) + 10)
                                    Position -= 2;
                                else
                                    IsScrolling = false;
                        }
                    }
                    else
                    {
                        IsScrolling = true;
                        AutoScroll(index1, list1, index2 + 1, list2);
                    }
                }
                else
                {
                    IsScrolling = true;
                    AutoScroll(index1 + 1, list1, index2, list2);
                }
            }
            else
            {
                if (list2.Count() == 0 && list1.Count() > 0)
                {
                    if (Position + (ViewWidth / 2) < list1[index1].Position.X - 30)
                        Position += 1;
                    else
                        if (Position + (ViewWidth / 2) > list1[index1].Position.X + 30)
                            Position -= 1;
                        else
                            IsScrolling = false;
                    return;
                }
                if (list1.Count() == 0 && list2.Count() > 0)
                {
                    if (Position + (ViewWidth / 2) < list2[index2].Position.X - 30)
                        Position += 1;
                    else
                        if (Position + (ViewWidth / 2) > list2[index2].Position.X + 30)
                            Position -= 1;
                        else
                            IsScrolling = false;
                    return;
                }
            }
        }
    }
}
